package com.example.aircraft.base;

import android.media.MediaPlayer;

import com.example.aircraft.MyApplication;

/**
 * 音乐播放线程类
 * 生命周期短，主要负责短音效播放
 */
public class MusicThread extends Thread{
    private MediaPlayer player;
    private int resId;
    /**
     * @param resId 资源索引序号
     */
    public MusicThread(int resId){
        player = MediaPlayer.create(MyApplication.getContext(),resId);
    }
    @Override
    public void run(){
        super.run();
        if(!player.isPlaying()){
            // 开始播放
            player.start();
            // 一般不允许循环播放
            player.setLooping(false);
        }
    }
    @Override
    public void interrupt(){
        super.interrupt();
        if(player.isPlaying()){
        player.stop();}
        player.release();
    }
}
